<%@page contentType="text/html; charset=utf-8"%>
<%@include file="/WEB-INF/views/common/tags.jspf" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>资源管理</title>
	<%@include file="/WEB-INF/views/common/header.jspf" %>
</head>

<body>

	<div id="tg">
		<div id="toolbars">
			<form id="searchForm">
				<div class="search-bar">
					<div class="bar-row">
						<button type="button" onclick="form_dialog('add');" class="btn btn-green btn-xs height-25 mt-3 float-left"><div class='icon-div'><i class='fa fa-plus fa-120 pt-2'></i></div> 新增</button>
						<button type="button" onclick="form_dialog('update');" class="btn btn-blue btn-xs height-25 mt-3 ml-1 float-left"><div class='icon-div'><i class='fa fa-pencil fa-120 pt-2'></i></div> 编辑</button>
						<button type="button" onclick="del();" class="btn btn-red btn-xs height-25 mt-3 ml-1 float-left"><div class='icon-div'><i class='fa fa-trash fa-120 pt-2'></i></div> 删除</button>
						<button type="button" onclick="reload();" class="btn btn-purple1 btn-xs height-25 mt-3 ml-1 float-left"><div class='icon-div'><i class='fa fa-refresh fa-120 pt-2'></i></div> 刷新</button>
						
						<button type="button" id="filterType" value="1" class="btn btn-blue1 btn-xs height-25 mt-3 ml-1 float-left"><div class='icon-div'><i class='fa fa-gears fa-120 pt-2'></i></div> 过滤操作</button>
						<div class="bar-input ml-10">
		            		<input id="menu" class="easyui-validatebox" type="text" style="width: 150px; height: 25px;" />
	            		</div>
					</div>
				</div>
			</form>
		</div>
	</div>
	
	
	<%@include file="/WEB-INF/views/common/script.jspf" %>
	<script>
	var $tg, $menu ;
	$(function(){
		loadResource() ;
		$menu = $("#menu").combobox({
			url: "${ctx}/admin/sys/menu/listAll",
			valueField: "id", textField: "name", panelHeight: "auto",
			onSelect: function(record) {
				$("#filterType").val(1) ;
				$tg.treegrid({url: "${ctx}/admin/sys/resource/tree?menuId="+record.id, queryParams: {"filterType": true}}) ;
			},
			onLoadSuccess: function() {
				var data= $menu.combobox("getData") ;
				if(data.length > 0) {
					$menu.combobox("setValue", data[0].id);
					$tg.treegrid({url: "${ctx}/admin/sys/resource/tree?menuId="+data[0].id, queryParams: {"filterType": true}}) ;
				} else {
					$("button").attr("onclick", "").click(function(){
						alertify.warning("目前没有菜单，请先创建菜单！");
					}); ;
				}
			}
		});
		

		//过滤操作
		$("#filterType").click(function(){
			var val = $(this).val() ;
			if(val%2==1) {
				$(this).text("显示操作");
				$tg.treegrid("load", {"filterType": false}) ;
			} else {
				$(this).text("隐藏操作");
				$tg.treegrid("load", {"filterType": true}) ;
			}
			$(this).val((parseInt(val)+1));
		});
	});
	function reload(){$tg.treegrid("reload") ;}
	
	function loadResource() {
		$tg = $("#tg").treegrid({
			title: "资源管理",
            idField: "id", treeField: "name", fit: true, 
            border: false, striped:true, singleSelect: true,rownumbers: true, 
            toolbar: "#toolbars", sortName: 'weight', sortOrder: 'asc',
            frozenColumns: [[
                { field: "ck", checkbox: true },
                { field: "id", title: "ID", width: 50, sortable: true }
            ]],
            columns: [[
                { field: "name", title: "资源名称", width: 180 },
                { field: "weight", title: "排序", width: 50, align: "center", sortable: true },
                { field: "href", title: "资源地址", width: 300 },
                { field: "identity", title: "权限标识", width: 100 },
                { field: "parentIds", title: "父节点路径", width: 100 },
                { field: "parentIdentity", title: "父标识路径", width: 200 },
                { field: "createTime", title: "创建时间", width: 150, sortable: true }
            ]],
            onLoadSuccess: function(){
            	$.fn.treegrid.extensions.onLoadSuccess.apply(this, arguments);
            	$tg.treegrid('unselectAll');$tg.treegrid('clearSelections');
            },
            enableHeaderClickMenu: true, enableHeaderContextMenu: true, enableRowContextMenu: false
        });
	}
	
	function form_dialog(formType) {
		var menuId = $menu.combobox("getValue");
		var form_url = "${ctx}/admin/sys/resource/form?menuId="+menuId;
		if(formType === "update") {
			var node = $tg.treegrid("getSelected");
			if(undefined == node || null == node) {alertify.warning("请选择一条记录！");return;}
			form_url = "${ctx}/admin/sys/resource/form?id="+node.id+"&menuId="+menuId;
		}
		
		var $d = $.easyui.showDialog({
			href: form_url, title: '<div class="icon-div"><i class="fa fa-link fa-120 pt-2 fa-c-blue"></i></div> 表单', iniframe: false, topMost: true, iconCls : '',
			width: 500, height: 440, maximizable: true,
            enableApplyButton: false, enableCloseButton: false, enableSaveButton: false,
            buttons : [ 
              { text : '<i class="fa fa-save fa-120 mt-6 fa-c-green"></i> 应用', handler : function() { $.easyui.parent.submitForm($d, $tg, true) ; } },
              { text : '<i class="fa fa-save fa-120 mt-6 fa-c-blue"></i> 保存', handler : function() { $.easyui.parent.submitForm($d, $tg) ; } },
              { text : '<i class="fa fa-remove fa-120 mt-6 fa-c-red"></i> 关闭', handler : function() { $d.dialog('destroy'); } } 
           	]
        });
	}
	
	
	function del() {
		var node = $tg.treegrid("getSelected");
		if(undefined == node || null == node) {alertify.warning("请选择一条记录！");return;}
		$.messager.confirm("您确定要进行该操作？", function (c) { 
			if(c) {
				$.easyui.loading({ msg: "数据删除中，请稍等..."});
				deletedAjax("${ctx}/admin/sys/resource/delete/"+node.id) ;
			}
		});
	}
	function deletedAjax(url, params) {
		$.get(url, params, function(result) {
			if (result.status) {
				$tg.treegrid("unselectAll");$tg.treegrid("reload") ;
				alertify.success(result.message);
				$.easyui.loaded();
			} else {
				alertify.warning(result.message);
				$.easyui.loaded();
			}
		}, 'json').error(function() { $.easyui.loaded(); });
	}
	</script>
</body>
</html>

